package com.base.service;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.base.entity.bo.*;
import com.base.entity.vo.ExpertVo;
import com.base.entity.vo.SciencePersonVo;
import com.base.mapper.*;
import com.base.utils.CutPageBean;
import com.common.core.domain.entity.SysUser;
import com.common.utils.uuid.IdUtils;
import com.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class ExpertService {
    @Autowired
    ExpertMapper expertMapper;

    @Autowired
    SciencePersonMapper sciencePersonMapper;

    @Autowired
    ISysUserService iSysUserService;

    @Autowired
    UserMapper userMapper;

    @Autowired
    ExpertGroupRelateMapper expertGroupRelateMapper;

    /**角色正常状态*/
    public static final long ROLE_BIZ_KYRY_ID = 101;
    public static final long ROLE_BIZ_KYMS_ID = 102;
    public static final long ROLE_BIZ_KYCGLY_ID = 103;
    public static final long ROLE_BIZ_PSZJ_ID = 104;

    public List getExpertList(ExpertVo expertVo) {
        List<ExpertBo> list = expertMapper.getExpertList(expertVo);
        return list;
    }

    public List getExpertHaveList(ExpertVo expertVo) {
        List<ExpertBo> list = expertMapper.getExpertHaveList(expertVo);
        return list;
    }

    public CutPageBean<ExpertBo> selectExpertList(ExpertVo expertVo) {
        expertVo.setStart(expertVo.getPage(),expertVo.getPageSize());
        CutPageBean<ExpertBo> cutPage = new CutPageBean<>();
        List<ExpertBo> list = expertMapper.selectExpertList(expertVo);
        int count = expertMapper.selectExpertCount(expertVo);
        cutPage.initCutPage(count,expertVo.getPageSize(),list);
        return cutPage;
    }

    public  int deletExpert(ExpertBo expertBo) {
            String[] result = expertBo.getIdList();
            int row =0;
            for (int i = 0; i < result.length; i++) {
                ExpertVo expertVo=new ExpertVo();
                expertVo.setId(result[i]);
                List<ExpertBo> c=expertMapper.reviewerId(expertVo);
                if(c.size()>0){
                    row=2;
                    return row;
                }

                //删除sysuser
                List<ExpertBo> a= expertMapper.selectExpertDetails(expertVo);
                List<String> expertSource=a.stream().map(ExpertBo::getExpertSource).collect(Collectors.toList());
                if(expertSource.get(0).equals("0")){
                    SysUser list = expertMapper.userIdList(expertVo);
                    Long userId=list.getUserId();
                    iSysUserService.deleteUserById(userId);
                }

                //删除expertGroupRelate
                ExpertGroupRelateBo expertGroupRelateBo=new ExpertGroupRelateBo();
                expertGroupRelateBo.setExpertId(result[i]);
                ExpertGroupRelateBo ExpertIdCount=expertGroupRelateMapper.ExpertIdCount(expertGroupRelateBo);
                Integer b= Integer.valueOf(ExpertIdCount.getCount());
                if(b>0){
                    Map<String,Object> map=new HashMap<>();
                    map.put("EXPERT_ID",result[i]);
                    expertGroupRelateMapper.deleteByMap(map);
                }

                //删除expert
                Map<String,Object> map=new HashMap<>();
                map.put("ID",result[i]);
                row=expertMapper.deleteByMap(map);
            }
        return row;
    }

    public  int insertExpertList(ExpertBo expertBo) {
        Date date = new Date();
        SimpleDateFormat dateFormat= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        expertBo.setCreatedate(dateFormat.format(date));
        expertBo.setId(IdUtils.simpleUUID());
        expertBo.setSysUserId(expertBo.getId());
        expertMapper.insert(expertBo);
        SysUser user=new SysUser();
        user.setUserName(expertBo.getMobile());
        user.setNickName(expertBo.getName());
        user.setEmail(expertBo.getEmail());
        user.setPhonenumber(expertBo.getMobile());
        user.setSex(expertBo.getSexId());
        user.setUserRealId(expertBo.getId());
        user.setDeptRealId("dept100");
        user.setDeptId(100L);
        user.setDefaultRoleId(ROLE_BIZ_PSZJ_ID);
        int row=iSysUserService.insertUser(user);
        return row;
    }

    public int updateExpertList(ExpertBo expertBo) {
        Date date = new Date();
        SimpleDateFormat dateFormat= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        expertBo.setLasteditdate(dateFormat.format(date));
        int row=expertMapper.update(expertBo,new UpdateWrapper<ExpertBo>().eq("ID",expertBo.getId()));
        User user=new User();
        user.setUserRealId(expertBo.getId());
        user.setNickName(expertBo.getName());
        user.setEmail(expertBo.getEmail());
        user.setPhonenumber(expertBo.getMobile());
        user.setSex(expertBo.getSexId());
        userMapper.update(user,new UpdateWrapper<User>().eq("user_real_id",user.getUserRealId()));
        return row;
    }

    public  List selectExpertDetails(ExpertVo expertVo) {
        List<ExpertBo> list = expertMapper.selectExpertDetails(expertVo);
        return list;
    }

    public  int insertSchoolExpertList(ExpertVo expertVo) {
        ExpertBo expertBo=new ExpertBo();
        int row=0;
        List<SciencePersonBo> schoolExpertList = expertVo.getSchoolExpertList();
        for(int i = 0;i<schoolExpertList.size();i++){
            Date date = new Date();
            SimpleDateFormat dateFormat= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            expertBo.setCreatedate(dateFormat.format(date));
            expertBo.setId(IdUtils.simpleUUID());
            expertBo.setName(schoolExpertList.get(i).getName());
            expertBo.setSexId(schoolExpertList.get(i).getSexId());
            expertBo.setSubjectId(schoolExpertList.get(i).getSearchId());
            expertBo.setWorkUnit(schoolExpertList.get(i).getUnitName());
            expertBo.setEduDegree(schoolExpertList.get(i).getEduDegreeId());
            expertBo.setTitleId(schoolExpertList.get(i).getTitleId());
            expertBo.setPersonId(schoolExpertList.get(i).getId());
            expertBo.setSubjectClassId(schoolExpertList.get(i).getSearchClassId());
            expertBo.setBirthday(schoolExpertList.get(i).getBirthday());
            expertBo.setMobile(schoolExpertList.get(i).getMobile());
            expertBo.setTelOffice(schoolExpertList.get(i).getTelOffice());
            expertBo.setEmail(schoolExpertList.get(i).getEmail());
            expertBo.setResearchField(schoolExpertList.get(i).getResearchField());
            expertBo.setEduLevel(schoolExpertList.get(i).getEduLevelId());
            expertBo.setHonorTitleId(schoolExpertList.get(i).getHonorTitleId());
            expertBo.setAccount(schoolExpertList.get(i).getAccount());
            expertBo.setExpertSource("1");
            row=expertMapper.insert(expertBo);
        }
        return row;
    }

    public List getPersonHaveList(SciencePersonVo sciencePersonVo) {
        List<SciencePersonBo> list = sciencePersonMapper.getPersonHaveList(sciencePersonVo);
        return list;
    }

    public  List exportList(ExpertVo expertVo) {
        List<ExpertBo> list = expertMapper.exportList(expertVo);
        return list;
    }

}
